package com.tte.common.utils;

import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * HttpUtil
 */
@Component
public class HttpUtil {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private RestTemplate restTemplate;

    private JSONObject jsonObject;

    public JSONObject get(String urlStr, JSONObject params) {
        /**
         使用getForEntity：发起 get 请求。自己拼接参数字符串方式。
         这样的方式要使用 map 传递参数。
         如果有汉字要注意需要进行 urlencode。
         Map的key要和url中的占位符一致
         */
        String url = "http://127.0.0.1:8080/login?name={name}";
        Map<String, String> map = new HashMap<>();
        map.put("name", "join");
        ResponseEntity<String> result = restTemplate.getForEntity(url, String.class, map);
        return null;
    }

    public Object getForExecute(String urlStr, JSONObject params) {
        String url = "http://127.0.0.1:8080/rest/get";
        MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
        multiValueMap.add("name", "这是name");
        multiValueMap.add("id", "1");
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        URI uri = builder.queryParams(multiValueMap).build().encode().toUri();
        ResponseEntity<String> forEntity = restTemplate.getForEntity(uri, String.class);
        return forEntity.getBody();
    }

    public JSONObject pots(String urlStr, JSONObject params) {
        logger.debug("---HttpRestTemplate调用跨地区数据获取接口URL：{}" + urlStr);
        logger.debug("---HttpRestTemplate调用跨地区数据获取接口参数：{}" + params);
        ResponseEntity<JSONObject> responseEntity = null;
        try {
            RestTemplate restTemplate = new RestTemplate();
            String jsonObject = null;
            if (null != params) {
                jsonObject = params.toString();
            }
            logger.debug("---HttpRestTemplate参数字符串：{}" + params);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            HttpEntity<String> entity = new HttpEntity<>(jsonObject, headers);
            responseEntity = restTemplate.postForEntity(urlStr, entity, JSONObject.class);
            if (null == responseEntity || responseEntity.getStatusCode().value() != 200) {
                logger.error("---HttpRestTemplate调用失败：{}", responseEntity);
            }
            if (MapUtils.isNotEmpty(responseEntity.getBody())) {
                Map<String, Object> responseData = responseEntity.getBody();
                logger.error("---HttpRestTemplate调用成功：{}", responseData);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("HttpRestTemplate调用异常： {}", e);
            return null;
        }
        return responseEntity.getBody();
    }

    public JSONObject potsForExecute(String urlStr, JSONObject params) {
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
//        headers.set(HttpHeaders.COOKIE, cookie);

        // 请求体
        JSONObject body = new JSONObject();
        HttpEntity<String> request = new HttpEntity(body.toString(), headers);
        JSONObject result = restTemplate.postForObject(urlStr, request, JSONObject.class);
        return result;
    }


}
